package com.gitee.shigen.sample.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.shigen.api.feign.user.vo.UserVO;
import com.gitee.shigen.sample.entity.User;
import com.gitee.shigen.sample.mapper.UserMapper;
import com.gitee.shigen.sample.service.IUserService;

import cn.hutool.core.util.StrUtil;

/**
 * @author shigenfu
 * @date 2023/2/25 11:00 下午
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public int insert(User user) {
        return getBaseMapper().insert(user);
    }

    @Override
    public int delete(Long id) {
        return getBaseMapper().deleteById(id);
    }

    @Override
    public int update(User user) {
        return getBaseMapper().updateById(user);
    }

    @Override
    public UserVO query(Long id) {
        User user = getBaseMapper().selectById(id);
        Assert.notNull(user, "用户不存在");
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @Override
    public UserVO loadUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(username), User::getUsername, username);
        User user = getBaseMapper().selectOne(queryWrapper);
        Assert.notNull(user, "用户不存在");
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }
}
